<?php

class jwGalleryPicture extends BasejwGalleryPicture
{
    public function doSave( PropelPDO $con )
    {
        if ( $this->isNew() && $this->getjwGallery() )
        {
            $tmp_dir = sfContext::getInstance()->getUser()->getAttribute( 'tmp_dir', null, 'uploads' );
            $root = sfConfig::get('sf_root_dir');
            
            $srcGallery = $root.DIRECTORY_SEPARATOR."galleries".DIRECTORY_SEPARATOR.$tmp_dir;
            $trgGallery = $root.DIRECTORY_SEPARATOR."galleries".DIRECTORY_SEPARATOR.$this->getjwGallery()->getPath();
            
            if ( !is_dir( $trgGallery ) )
                mkdir( $trgGallery );
            
            $thumbSrcDir = $root.DIRECTORY_SEPARATOR."galleries".DIRECTORY_SEPARATOR.$tmp_dir.DIRECTORY_SEPARATOR."thumbs";
            $thumbTrgDir = $root.DIRECTORY_SEPARATOR."galleries".DIRECTORY_SEPARATOR.$this->getjwGallery()->getPath().DIRECTORY_SEPARATOR."thumbs";
            
            if ( !is_dir( $thumbTrgDir ) )
                mkdir( $thumbTrgDir );                    
            
            $limits = sfConfig::get( 'app_jw_gallery' );
            $editWidth = $editHeight = $limits['edit'];
            
            if ( !is_dir( $thumbTrgDir.DIRECTORY_SEPARATOR.$editWidth ) )
                mkdir( $thumbTrgDir.DIRECTORY_SEPARATOR.$editWidth );                
            
            rename( $srcGallery.DIRECTORY_SEPARATOR.$this->getFileName(), $trgGallery.DIRECTORY_SEPARATOR.$this->getFileName() );
            
            rename( $thumbSrcDir.DIRECTORY_SEPARATOR.$editWidth.DIRECTORY_SEPARATOR.$this->getFileName(), $thumbTrgDir.DIRECTORY_SEPARATOR.$editWidth.DIRECTORY_SEPARATOR.$this->getFileName() );

            foreach( $limits['thumbnails'] as $size )
            {
                $thumbWidth = $thumbHeight = $size;
            
                if ( !is_dir( $thumbTrgDir.DIRECTORY_SEPARATOR.$thumbWidth ) )
                {
                    mkdir( $thumbTrgDir.DIRECTORY_SEPARATOR.$thumbWidth );
                }
                
                $thumb = new sfThumbnail( $thumbWidth, $thumbHeight );
                $thumb->loadFile( $trgGallery.DIRECTORY_SEPARATOR.$this->getFileName() );                
                $thumb->save( $thumbTrgDir.DIRECTORY_SEPARATOR.$thumbWidth.DIRECTORY_SEPARATOR.$this->getFileName() );
            }
        }
        parent::doSave( $con );
    }
    
    public function delete(PropelPDO $con = null)
    {        
        parent::delete( $con );

        if ( $this->isDeleted() )
        {
            $limits = sfConfig::get( 'app_jw_gallery' );
            $eWidth = $eHeight = $limits['edit'];
            
            $path = $this->getjwGallery()->getPath();
            $rootDir = sfConfig::get( 'sf_root_dir');
            $baseDir = $rootDir.DIRECTORY_SEPARATOR.'galleries'.DIRECTORY_SEPARATOR.$path;
            if( file_exists( $baseDir.DIRECTORY_SEPARATOR.$this->getFilename() ) )
                unlink( $baseDir.DIRECTORY_SEPARATOR.$this->getFilename() );
            if( file_exists( $baseDir.DIRECTORY_SEPARATOR."thumbs".DIRECTORY_SEPARATOR.$eWidth.DIRECTORY_SEPARATOR.$this->getFilename() ) )
                unlink( $baseDir.DIRECTORY_SEPARATOR."thumbs".DIRECTORY_SEPARATOR.$eWidth.DIRECTORY_SEPARATOR.$this->getFilename() );
                
            foreach( $limits['thumbnails'] as $size )
            {
                $gWidth = $gHeight = $size;
                
                if( file_exists( $baseDir.DIRECTORY_SEPARATOR."thumbs".DIRECTORY_SEPARATOR.$gWidth.DIRECTORY_SEPARATOR.$this->getFilename() ) )
                    unlink( $baseDir.DIRECTORY_SEPARATOR."thumbs".DIRECTORY_SEPARATOR.$gWidth.DIRECTORY_SEPARATOR.$this->getFilename() );
            }
        }
    }
}
